package com.ruoyi.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.bo.PStoreCartBo;
import com.ruoyi.system.domain.vo.PStoreCartVo;
import com.ruoyi.system.service.IPStoreCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 购物车Controller
 *
 * @author ruoyi
 * @date 2022-08-01
 */
@Validated
@Api(value = "购物车控制器", tags = {"购物车管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/storeCart")
public class PStoreCartController extends BaseController {

    private final IPStoreCartService iPStoreCartService;

    /**
     * 查询购物车列表
     */
    @ApiOperation("查询购物车列表")
    @SaCheckPermission("system:storeCart:list")
    @GetMapping("/list")
    public TableDataInfo<PStoreCartVo> list(PStoreCartBo bo, PageQuery pageQuery) {
        return iPStoreCartService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出购物车列表
     */
    @ApiOperation("导出购物车列表")
    @SaCheckPermission("system:storeCart:export")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PStoreCartBo bo, HttpServletResponse response) {
        List<PStoreCartVo> list = iPStoreCartService.queryList(bo);
        ExcelUtil.exportExcel(list, "购物车", PStoreCartVo.class, response);
    }

    /**
     * 获取购物车详细信息
     */
    @ApiOperation("获取购物车详细信息")
    @SaCheckPermission("system:storeCart:query")
    @GetMapping("/{id}")
    public R<PStoreCartVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") Integer id) {
        return R.ok(iPStoreCartService.queryById(id));
    }

    /**
     * 新增购物车
     */
    @ApiOperation("新增购物车")
    @SaCheckPermission("system:storeCart:add")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PStoreCartBo bo) {
        return R.ok(iPStoreCartService.insertByBo(bo));
    }

    /**
     * 修改购物车
     */
    @ApiOperation("修改购物车")
    @SaCheckPermission("system:storeCart:edit")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PStoreCartBo bo) {
        return toAjax(iPStoreCartService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除购物车
     */
    @ApiOperation("删除购物车")
    @SaCheckPermission("system:storeCart:remove")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iPStoreCartService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
